package mk.com.ldm.corepoint.implementation.client.lib.wrappers.widgets;

import java.util.Date;

import org.gwt.mosaic.ui.client.InfoPanel;

import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseWidget;
import mk.com.ldm.corepoint.implementation.client.lib.validation.Validator;
import mk.com.ldm.corepoint.implementation.client.presenter.EventHandler;
import mk.com.ldm.corepoint.implementation.shared.FormRow;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.datepicker.client.DateBox;

public class DateBoxWrapper extends BaseWidget {

	private DateBox datebox = new DateBox();
	private Label label = new Label();
	private FormRow config;
		
	public DateBoxWrapper(FormRow config, HandlerManager eventBus) {
		super(config);
		
		this.config = config;
		
		this.setType(7);
		label.setText(config.getNam());
		label.setWidth(this.LABEL_WIDTH);
		datebox.setTitle(config.getNam().replace(":",""));
		datebox.getTextBox().setName(config.getNam());
		datebox.setTabIndex(config.getTab());
		datebox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("ddMMyyyy")));
		
		if(!config.getVal().isEmpty()){
			//TODO: validate as date before input
			datebox.getTextBox().setValue(config.getVal());
		}
		if(!config.getSiz().equals("")){
			datebox.setWidth(config.getSiz().split(",")[0]+"px");
			if(config.getSiz().split(",").length > 1){
				datebox.setHeight(config.getSiz().split(",")[1]+"px");
			}
		}
		if(config.getEna() == 0) datebox.setEnabled(false);
		if(config.getVis() == 0){
			datebox.setVisible(false);
			label.setVisible(false);
		}
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		hp.add(label);
		hp.add(datebox);
		
		if(!config.getEts().isEmpty()){
			setEvents(config.getEts());
		}
		
		initWidget(hp);
		
	}
	
	public void setEvents(String events){
		String eventList[] = events.split(",");
		
		for(int i=0; i<eventList.length; i++){
			final String eventType = eventList[i].split(":")[0];
			final String eventId = eventList[i].split(":")[1];
			
			if(eventType.equals("change")){
				datebox.addValueChangeHandler(new ValueChangeHandler<Date>(){
					@Override
					public void onValueChange(ValueChangeEvent<Date> event) {
						EventHandler.getInstance().fireEvent(eventType, eventId);						
					}
				});
			}
		}
		
	}

	public DateBox getDatebox() {
		return datebox;
	}

	public void setDatebox(DateBox datebox) {
		this.datebox = datebox;
	}

	public Label getLabel() {
		return label;
	}

	public void setLabel(Label label) {
		this.label = label;
	}
	
	@Override
	public void setValue(String value){ datebox.getTextBox().setValue(value);}
	@Override
	public String getValue(){ return datebox.getTextBox().getValue(); }
	@Override
	public void setEnabled(Boolean enabled) {datebox.setEnabled(enabled);}
	@Override
	public void setVisible(Boolean visible) {datebox.setVisible(visible); label.setVisible(visible);}
	@Override
	public void setTitle(String title) {datebox.setTitle(title);}
	@Override
	public String getTitle() {return datebox.getTitle();}
	@Override
	public String getText() {return label.getText();}
	@Override
	public void setText(String text) { label.setText(text); }
	@Override 
	public String getName(){ return datebox.getTextBox().getName(); }
	@Override
	public void setName(String name){ datebox.getTextBox().setName(name);}
	@Override
	public void setFocus(boolean focus){ datebox.setFocus(focus);}
	@Override
	public String getStyleName(){
		return datebox.getStyleName();
	}

}
